import 'package:efood_multivendor/controller/transaction_controller.dart';

import 'package:efood_multivendor/data/model/response/transaction_model.dart';
import 'package:efood_multivendor/helper/date_converter.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';

import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';

import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_app_bar.dart';

import 'package:efood_multivendor/view/base/no_data_screen.dart';

import 'package:efood_multivendor/view/screens/wallet/widget/transaction_shimmer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

class TransactionDetails extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final ScrollController scrollController = ScrollController();

    return Scaffold(
      appBar: CustomAppBar(title: 'transaction_history'.tr),
      backgroundColor: Colors.white,
      body: GetBuilder<TransactionController>(builder: (transactionController) {
        List<TransactionModel>? transactionList;
        bool paginate = false;
        int pageSize = 1;
        int offset = 1;

        if (transactionController.historyTransactionList != null) {
          transactionList = transactionController.historyTransactionList;
          paginate = transactionController.historyPaginate;
          pageSize = (transactionController.historyPageSize / 10).ceil();
          offset = transactionController.historyOffset;
        }

        scrollController.addListener(() {
          if (scrollController.position.pixels ==
                  scrollController.position.maxScrollExtent &&
              transactionList != null &&
              !paginate) {
            if (offset < pageSize) {
              Get.find<TransactionController>().setOffset(offset + 1);
              Get.find<TransactionController>().showBottomLoader();

              Get.find<TransactionController>()
                  .getTransactionDetails(offset + 1);
            }
          }
        });

        return transactionList != null
            ? transactionList.length > 0
                ? RefreshIndicator(
                    onRefresh: () async {
                      await transactionController.getTransactionDetails(1);
                    },
                    child: SizedBox(
                      width: Dimensions.WEB_MAX_WIDTH,
                      child: SingleChildScrollView(
                        controller: scrollController,
                        physics: AlwaysScrollableScrollPhysics(),
                        child: Column(
                          children: [
                            ListView.separated(
                              separatorBuilder:
                                  (BuildContext context, int index) => Padding(
                                padding: EdgeInsets.only(left: 3.w, right: 2.w),
                                child: Divider(
                                  color: Theme.of(context).disabledColor,
                                  height: Dimensions.PADDING_SIZE_LARGE,
                                ),
                              ),
                              padding:
                                  EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
                              itemCount: transactionList.length + 1,
                              physics: NeverScrollableScrollPhysics(),
                              shrinkWrap: true,
                              itemBuilder: (context, index) {
                                if (index != transactionList?.length) {
                                  return Padding(
                                    padding: EdgeInsets.only(right: 2.w),
                                    child: Container(
                                      child: Column(children: [
                                        Row(children: [
                                          SizedBox(
                                              width: Dimensions
                                                  .PADDING_SIZE_SMALL),
                                          Expanded(
                                            child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Row(children: [
                                                    SizedBox(
                                                        width: Dimensions
                                                            .PADDING_SIZE_EXTRA_SMALL),
                                                    Text(
                                                        transactionList?[index]
                                                                .transactionType
                                                                ?.tr ??
                                                            '',
                                                        style: sfMedium.copyWith(
                                                            fontSize: Dimensions
                                                                .fontSizeDefault))
                                                  ]),
                                                  SizedBox(height: 1.5.h),
                                                  Padding(
                                                    padding: EdgeInsets.only(
                                                        left: 1.5.w),
                                                    child: Text(
                                                      DateConverter
                                                          .convertUtcToEat(
                                                              transactionList![
                                                                          index]
                                                                      .createdAt ??
                                                                  ''),
                                                      style: sfRegular.copyWith(
                                                          color: Theme.of(
                                                                  context)
                                                              .disabledColor,
                                                          fontSize: Dimensions
                                                              .fontSizeSmall),
                                                    ),
                                                  ),
                                                ]),
                                          ),
                                          SizedBox(
                                              width: Dimensions
                                                  .PADDING_SIZE_SMALL),
                                          Expanded(
                                            child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.end,
                                                children: [
                                                  Padding(
                                                      padding: EdgeInsets.only(
                                                          right: 0.5.w),
                                                      child:
                                                          transactionList[index]
                                                                      .debit! >
                                                                  0
                                                              ? Text(
                                                                  '- ${transactionList[index].debit} Pts',
                                                                  style: TextStyle(
                                                                      fontSize:
                                                                          Dimensions
                                                                              .fontSizeDefault,
                                                                      color: Colors
                                                                          .red),
                                                                )
                                                              : Text(
                                                                  '+ ${transactionList[index].credit} Pts',
                                                                  style: TextStyle(
                                                                      fontSize:
                                                                          Dimensions
                                                                              .fontSizeDefault,
                                                                      color: Colors
                                                                          .green),
                                                                )),
                                                ]),
                                          ),
                                        ]),
                                      ]),
                                    ),
                                  );
                                } else {
                                  return SizedBox(
                                    height: 10.h,
                                  );
                                }
                              },
                            ),
                            paginate
                                ? Padding(
                                    padding: EdgeInsets.only(bottom: 12.h),
                                    child: Center(
                                      child: CircularProgressIndicator.adaptive(
                                        backgroundColor: Theme.of(context)
                                            .colorScheme
                                            .secondary,
                                      ),
                                    ),
                                  )
                                : SizedBox(),
                          ],
                        ),
                      ),
                    ))
                : NoDataScreen(
                    text: 'no_transaction'.tr,
                    isTransaction: true,
                  )
            : TransactionShimmer(transactionController: transactionController);
      }),
    );
  }
}
